package com.hrms.service.impl;

import com.hrms.domain.User;
import com.hrms.domain.web.LoginInfo;
import com.hrms.mapper.UserMapper;
import com.hrms.service.IUserService;
import com.hrms.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @author:zhao xinyu
 * @date: 2024/12/19 - 09:07
 * @mail: zxy1277692924@outlook.com
 * @info:
 */
@Service
public class UserServiceImpl implements IUserService
{
    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginInfo login(User user) {
        // 根据用户名和密码查询用户信息
        // TODO 此时要思考，密码是明文还是密文，如果是明文应该怎么修改
        User loginUser = userMapper.selectUserByUsernameAndPassword(user);
        if(loginUser != null){
            //1. 生成JWT令牌
            Map<String,Object> dataMap = new HashMap<>();
            dataMap.put("empNo", loginUser.getEmpNo());
            dataMap.put("empName", loginUser.getEmpName());
            dataMap.put("username", loginUser.getUsername());

            String jwt = JwtUtils.generateJwt(dataMap);

//            LoginInfo loginInfo = new LoginInfo(loginUser.getEmpNo(), loginUser.getUsername(),loginUser.getEmpName(), jwt);
            LoginInfo loginInfo = new LoginInfo(Integer.parseInt(String.valueOf(loginUser.getEmpNo())), loginUser.getUsername(),loginUser.getEmpName(), jwt);
            return loginInfo;
        }
        return null;
    }
}